<template>
  <div class="app-container">

    <!-- 工具按钮 -->
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets" style="margin-top: 5px"></i>
      <span style="margin-top: 5px">数据列表</span>
      <el-button class="btn-add" @click="batchRemove()" >批量删除</el-button>
    </el-card>
    <!-- 表格 -->
    <el-table
      :data="list"
      border
      stripe
      @selection-change="handleSelectionChange">
      <el-table-column type="selection"/>
      <el-table-column prop="id" label="id" width="50" />
      <el-table-column prop="subjectinfo" label="题目" width="400" />
      <el-table-column label="A选项" prop="subjecta" width="100"></el-table-column>
      <el-table-column prop="subjectb" label="B选项" width="100" />
      <el-table-column prop="subjectc" label="C选项" width="100" />
      <el-table-column prop="subjectd" label="D选项" width="100" />
      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="removeById(scope.row.id)">删除</el-button>
          <!-- <el-button type="text" size="mini" @click="reviseId(scope.row.id)">修改</el-button> -->

          <router-link :to="'/index/'+scope.row.id">
            <el-button type="text" size="mini"> 修改</el-button>
          </router-link>        
        </template>
      </el-table-column>
    </el-table>

      <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
      style="padding: 30px 0; text-align: center;"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="changePageSize"
      @current-change="changeCurrentPage"
    />
  </div>
</template>

<script>
import indexApi from '@/api/question/index.js'
import indexApi1 from '@/api/certification/index.js'
export default {
  // 定义数据模型
   data() {
    return {
      list: [], // 讲师列表
      total: 0, // 总记录数
      page: 1, // 页码
      limit: 5, // 每页记录数
      multipleSelection: []// 批量删除选中的记录列表

    }
  },
  created() { // 在渲染之前执行
        this.task()
        
        
  
    },
    mounted() { // 在渲染之后执行   
        this.fetchData()
       
    },
  
  // 定义方法
    methods: {
       task(){
            indexApi1.getemail().then(response => {
              const authority=response.data.authority
              if(authority===0){
                this.$alert('你不是管理员了不能进行这个操作', '权限认证', {
                    confirmButtonText: '确定',
                    callback: action => {
                        this.$router.push(`/login`)
                        }
                    })
                }
              })
          } ,

    fetchData() {
      // 调用api
      indexApi.pageList(this.page, this.limit).then(response => {
        this.list = response.data.records
        this.total = response.data.total
      })
    },
     // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
    changePageSize(size) {
      this.limit = size
      this.fetchData()
    },

    // 改变页码，page：回调参数，表示当前选中的“页码”
    changeCurrentPage(page) {
      this.page = page
      this.fetchData()
    },


    //根据id进行修改
    reviseId(id) {
      this.$router.push(`/index`,id)
    },

    //根据id删除该题
    removeById(id) {
      this.$confirm('此操作将永久删除该题, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return indexApi.removeById(id)
      }).then((response) => {
        this.fetchData()
        this.$message.success(response.message)
      })
    },
    
    handleSelectionChange(selection){
      this.multipleSelection=selection
    },



    batchRemove(){
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请选择要删除的记录！')
        return
      }
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 点击确定，远程调用ajax
        // 遍历selection，将id取出放入id列表
        var idList = []
        this.multipleSelection.forEach(item => {
          idList.push(item.id)
          console.log(idList)
        })
        // 调用api
        return indexApi.Batchdeleter(idList)
      }).then((response) => {
        this.fetchData()
        this.$message.success(response.message)
      }).catch(error => {
        if (error === 'cancel') {
          this.$message.info('取消删除')
        }
      })
    },
    
  }
}
</script>